//
//  MyDocumentController.m
//  This file is part of D&D Manager
//
//  Created by Matthew Hutton on 09/06/2007.
//  Copyright Matthew Hutton 2007.
//
//D&D Manager, a D&D Character and battle management program.
//Copyright (C) 2007, Matthew Hutton
//
//This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along with this program; if not, write to:
//The Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#import "MyDocumentController.h"

@implementation MyDocumentController
-(id)init{
	[super init];
	return self;
}
- (id)openUntitledDocumentAndDisplay:(BOOL)displayDocument error:(NSError **)error{
	//NSLog(@"exe name=%@",[[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleExecutable"]);
	NSString *currentPath=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"campaign"];
	NSString *newPath=[[currentPath substringToIndex:[currentPath rangeOfString:@"/" options:NSBackwardsSearch].location] stringByAppendingFormat:@"/Data%lf.campaign",[NSDate timeIntervalSinceReferenceDate]];
	//make a copy of url.
	[[NSFileManager defaultManager] copyPath:currentPath toPath:newPath handler:nil];
	NSPersistentDocument *newDocument=(NSPersistentDocument *)[self makeDocumentForURL:NULL withContentsOfURL:[NSURL fileURLWithPath:newPath] ofType:[self defaultType] error:error];
	if(newDocument==nil){
		NSLog(@"newDocument is nil, error=%@",*error);
		return nil;
	}
	NSError *configError=nil;
	//[storeCoord removePersistentStore:[[storeCoord persistentStores] objectAtIndex:0] error:&configError];
	if(configError!=nil){
		NSLog(@"config error=%@",configError);
	}
	[newDocument updateChangeCount:NSChangeCleared];
	[self addDocument:newDocument];
	if(displayDocument){
		[newDocument makeWindowControllers];
		[newDocument showWindows];
	}
	return newDocument;
}
- (id)openDocumentWithContentsOfURL:(NSURL *)absoluteURL display:(BOOL)displayDocument error:(NSError **)outError{
	id object=[super openDocumentWithContentsOfURL:absoluteURL display:displayDocument error:outError];
	if(*outError!=nil){
		//check metadata
		NSError *error=nil;
		NSDictionary *dict=[NSPersistentStoreCoordinator metadataForPersistentStoreWithURL:absoluteURL error:&error];
		if([[[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleVersion"] intValue]<[[dict valueForKey:@"minSupportedVersion"] intValue]){
			//needs sticking in a different file.
			NSString *errorString=[NSString stringWithFormat:@"This version of D&D Manager (%@) is too old to open the file, as unfortunately version %@ is required.\n\nPlease download a newer version from\nhttp://www.erasersoft.com",[[[NSBundle mainBundle] infoDictionary]	valueForKey:@"CFBundleShortVersionString"],[dict valueForKey:@"shortMinSupportedVersion"]];
			*outError=[[NSError alloc] initWithDomain:@"Document Failed to Load, incompatable with this version of D&D Manager." code:-1 userInfo:[NSDictionary dictionaryWithObject:errorString forKey:NSLocalizedFailureReasonErrorKey]];
			return nil;
		}
	}
	return object;
}
@end
